﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PrototypeZGame
{
    public class Item : Actor
    {
        public Item(int X, int Y, World world)
            : base(X, Y, world)
        {
            tileset = world.tileset;
            _blocking = Result.BlockingItem;
        }

        //public override void Draw()
        //{
        //    Rectangle destinationRectangle = new Rectangle(position.x * fieldResolution, position.y * fieldResolution, fieldResolution, fieldResolution);
        //    Rectangle sourceRectangle = new Rectangle(((int)tileType % 16) * fieldResolution, fieldResolution * ((int)tileType / 16), fieldResolution, fieldResolution);
        //    world.spriteBatch.Draw(tileset, destinationRectangle, sourceRectangle, Color.White);
        //}

    }


    public class Door : Item
    {
        public enum State
        {
            Open,
            Closed,
            Broken,
        }
        public State state;

        public float HP, maxHP;

        public Door(int X, int Y, World world)
            : base(X, Y, world)
        {
            tileType = TileType.DoorClosed;
            tileset = world.tileset;
            _blocking = Result.BlockingDoor;

            name = "Door";
            if (world.rand.Next() % 2 == 0)
            {
                open();
            }
            else
            {
                close();
            }
            _moveCost = 1;

            HP = 10;
            maxHP = 10;
        }

        public override int moveCost
        {
            get
            {
                if (_blocking != Result.NoBlock)
                {
                    return _moveCost;
                }
                else
                {
                    return 0;
                }
                
            }

        }

        public void open() 
        {
            if (state != State.Broken)
            {
                _blocking = Result.NoBlock;
                tileType = TileType.DoorOpened;
                state = State.Open;
            }
        }

        public void close()
        {
            if (state != State.Broken)
            {
                if (terrain.blocking(position) == Result.NoBlock)
                {
                    _blocking = Result.BlockingDoor;
                    tileType = TileType.DoorClosed;
                }
            }
        }

        public void toggle()
        {
            if (_blocking == Result.NoBlock)
            {
                close(); 
            }
            else
            {
                open();
            }
        }

        public void Attacked(Unit attacker, float dmg)
        {
            HP -= dmg;
            if (HP <= 0)
            {
                state = State.Broken;
                _blocking = Result.NoBlock;
                tileType = TileType.WoodBroken;
            }
        }
    }

    public class Tree : Item
    {
        public Tree(int X, int Y, World world)
            : base(X, Y, world)
        {
            tileType = TileType.Tree;
            _moveCost = 1000;
        }
    }

    public class Shelves : Item
    {
        public Shelves(int X, int Y, World world)
            : base(X, Y, world)
        {
            tileType = TileType.Shelves;
            _moveCost = 1000;
        }
    }


}
